//
//  NewsCateListController.swift
//  WxToutiao
//
//  Created by YQ05 on 2018/4/12.
//  Copyright © 2018年 yons. All rights reserved.
//

import UIKit

class NewsCateListController: UITableViewController {
    

    //var newsList2 = ["示例新闻2","新闻3"]
    var newsList2:[NewsData2] = []
    var parentNavi : UINavigationController?
    
    var id = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        //newsList2.append(title!)
      
        getData()
    }
    
    func getData()  {

        NewsData2.request(id: id) { (data) in

            
            if let data = data {

                OperationQueue.main.addOperation {
                    self.newsList2 = data
                    self.tableView.reloadData()
                    self.refreshControl?.endRefreshing()
                }

                print("newscalist接收到的分类ID",self.self.id)
                
            } else {
                print("NewsCateListController网络错误")
            }
        }
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

//    override func numberOfSections(in tableView: UITableView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 0
//    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return newsList2.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewsListCell", for: indexPath) as! NewsListCell

        let news = newsList2[indexPath.row]
        
         //cell.NewstitleLabel.text = newsList2[0]
        
        cell.NewstitleLabel.text = news.title!
        print(news.title!)
        cell.NewsdateLabel.text = news.modified
        
        
        let originImg = news.image!
        let safeImg = originImg.addingPercentEncoding(withAllowedCharacters: NSCharacterSet(charactersIn:"`#%^{}\"[]|\\<> ").inverted)!
        
        /*从扩展函数拿图片*/
        if let url = URL(string: safeImg) {
            cell.NewsimgView.downloadedFrom(url: url)
        }
        /*从扩展函数拿图片*/
        
        //headerImageView.image = UIImage(named: imageName)
        //cell.NewsimgView.image = UIImage(named: nimg)
        //cell.NewsimgView.image = UIImage(named: "c30d6baecd2bdbdcea52c6d77784665b")
        
        // 方法一: 同步加载网络图片
//        //let url = URL(string: "http://nhahomenews.com/wp-content/uploads/2017/08/FotoJet-2.jpg")
//        let url = URL(string:nimg)
//        // 从url上获取内容
//        // 获取内容结束才进行下一步
//        let data = try? Data(contentsOf: url!)
//
//        if let imageData = data {
//            let imagen = UIImage(data: data!)
//            //loadImageFromURL.image = image
//            cell.NewsimgView.image = imagen
//        }
        
        
        
        
     
        
        // Configure the cell...

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let news = newsList2[tableView.indexPathForSelectedRow!.row]
        
        
        let detailVC = storyboard?.instantiateViewController(withIdentifier: "SBidhNewsDetail") as! hNewsDetailController
        
        //detailVC.title = title! + "新闻详情"
        
        detailVC.title = news.title
        detailVC.post = news
       
        
        parentNavi?.pushViewController(detailVC, animated: true)
    }
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    

    
    
    
    

}
